﻿using MovingChameLibrary.Models;
using MovingChameLibrary.ScheduleServiceReference;
using MovingChameLibrary.ServiceManager;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;

namespace MovingChameLibrary.ViewModels
{
    public class CurrentTourMapViewModel : BaseViewModel
    {
        private Tour _tour;
        public Tour Tour
        {
            get { return this._tour; }
            set { SetProperty(ref this._tour, value, "Tour"); }
        }

        public CurrentTourMapViewModel(int tourId)
        {
            ServiceFactory.GetTourServiceClient().GetTourAsync(tourId);
            ServiceFactory.GetTourServiceClient().GetTourCompleted += CurrentTourViewModel_GetTourCompleted;
        }

        private void CurrentTourViewModel_GetTourCompleted(object sender, TourServiceReference.GetTourCompletedEventArgs e)
        {
            Tour = DataConverter.ConvertToTour(e.Result);
            ServiceFactory.GetScheduleServiceClient().GetTourScheduleAsync(_tour.Id);
            ServiceFactory.GetScheduleServiceClient().GetTourScheduleCompleted += CurrentTourViewModel_GetTourScheduleCompleted;
        }

        private void CurrentTourViewModel_GetTourScheduleCompleted(object sender, ScheduleServiceReference.GetTourScheduleCompletedEventArgs e)
        {
            this._items = new CustomObservableCollection<Schedule>();
            foreach (ScheduleData scheduleData in e.Result)
            {
                this._items.Add(DataConverter.ConvertToSchedule(scheduleData, _tour));
            }
        }

        private CustomObservableCollection<Schedule> _items;
        public CustomObservableCollection<Schedule> Items
        {
            get
            {
                if (this._items == null)
                    this._items = new CustomObservableCollection<Schedule>();
                return this._items;
            }
        }
    }
}